/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

// module
import Taro, { Component } from '@tarojs/taro';
import { Swiper } from '@tarojs/components';

// project
import { CPage, CContent, CHeader, CFooter, CFloat, CAround, CBetween, CCenter, CRow, CWrap, CLine, CHand, CItemX, CItemY, CText, CLabel, CSvg, CImage, CButton, CColumn } from '../../library/comp';
import { Base64, Gen, Time, Zip, Session, Storage, Toast} from '../../library/func';
import Icon from '../../project/style/icon';
import Http from '../../project/func/http/http';
import '../../library/style/patch.css';

// 引用组件
import { Addcbtn, Editcunit } from '../../project/comp';

/******************************************************************************************
 * Export Class
 ******************************************************************************************/


export default class EditIndex extends Component {
	constructor(props) {
		super(props);

		/**
		 * state
		 */
		this.state = {
			post_url:"",
			type:"",
			addactive:-1,
			coverSrc:'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1542628687189&di=5022ae04443483dd4ebe42484ecfc2c3&imgtype=0&src=http%3A%2F%2Fm.tuniucdn.com%2Ffb2%2Ft1%2FG1%2FM00%2FA7%2F22%2FCii-U1jI-3GIHWSoAAY3TTlDHnoAAI4BQP3s40ABjdl776_w640_h480_c1_t0.jpg',
		}
	}

	/*------------------------------------------------------------------------------------
	 * 事件处理
	 *-----------------------------------------------------------------------------------*/
    // onRedirect
    onRedirect = (url, e)=> {
        wx.redirectTo({
            url: url
        })
    }

    // onNavigate
    onNavigate = (url, e)=> {
		wx.navigateTo({
			url: url
		})
	}
	
	upload(){
		wx.chooseImage({
			success (res) {
				const tempFilePaths = res.tempFilePaths

				for(var key in tempFilePaths){

					Http.Upload({
						filePath:tempFilePaths[key],
						success:(res)=>{
							console.log('>>>>>>>>>>>>>>>>>>>>>>',res)
						}
					});
				}


			}
		})		  
	}
	
	//分类提交
	makeSetData(type){
		switch(type){
			
			case "article":
			this.setState({
				type:type,
				post_url:"article/logic/add.php",
			})
			break;
			
			case "class":
			this.setState({
				type:type,
				post_url:"class/logic/add.php",
			})
			break;


		}
	}

	//点击添加按钮
	onAdd(val){
		this.setState({
			addactive:val,
		})
	}


	/*------------------------------------------------------------------------------------
	* 声明周期
	*-----------------------------------------------------------------------------------*/
	componentWillMount () {
		this.makeSetData(this.$router.params.type);
	}	

    componentDidMount() {
		this.makeSetData(this.$router.params.type);
    }

	componentDidShow(){
		this.makeSetData(this.$router.params.type);
	}

	
	/**
	 * 渲染
	 */
	render() {
		return (
			<CPage>
				<CContent style='height:100vh;'>
					<CColumn class='f1' bg='#f2f2f6' mb='50'>

						{/* 头部封面 */}
						<CColumn class='w1' h='130' bg='#d7d7d7' rel>
							<Image style='width:100%;height:100%;position:absolute;' top='0' left='0' mode='aspectFill'  src={this.state.coverSrc} />
							<CHand size='20' color='#fff' mb='15' ml='10' mt='10' rel bold onClick={this.onNavigate.bind(this,'/screen/add/input?type=title')} >点击设置标题</CHand>
							<CHand abs right='10' top='100' size='14' w='74' color='#515151' bg='#fff' ra='8' h='20' x='center' onClick={this.changeCover} >更换封面</CHand>
						</CColumn>

						{/* 默认 添加内容功能 */}
						<Addcbtn class='w1'  style='margin-top:20rpx;' showBool={ this.state.addactive == -1 ? true : false} onAdd={ this.onAdd.bind(this,-1) }></Addcbtn>

						{/* 循环内容 添加内容 */}
						<Editcunit class='w1'></Editcunit>
						<Addcbtn class='w1'  showBool={ this.state.addactive == 0 ? true : false} onAdd={ this.onAdd.bind(this,0) }></Addcbtn>
						
					</CColumn>
				</CContent>
			</CPage>
		);
	}
}

